var recom = {
	init : function(merchant_id, case_id) {	
		$('.non-editable').find('input').each(function(index, elmt){
			$(this).attr('readonly', 'readonly');
		});
		
		$('.non-editable').find('select').each(function(index, elmt){
			$(this).attr('disabled', 'disabled');
		});
		
		$('select#case_outlet_opt').change(function() {
			var outlet_id = $(this).val();
			
			if(outlet_id.length > 0) {
				window.location.href = jq_main_url + "/NewApp.do?dispatch=recom_approval&m_id=" + encodeURIComponent(merchant_id) + "&c_id=" + encodeURIComponent(case_id) + "&o_id=" + encodeURIComponent(outlet_id) + "#case_outlet_opt";
			}
			else {
				window.location.href = jq_main_url + "/NewApp.do?dispatch=recom_approval&m_id=" + encodeURIComponent(merchant_id) + "&c_id=" + encodeURIComponent(case_id);
			}
		});
	},
	
	create_comment : function(case_id) {
		var elmt, postdata;
		
		postdata = 'case_id=' + encodeURIComponent(case_id);
		
		elmt = document.getElementById('comment');
		if(elmt.value.trim().length <= 0){
			mapUI.jq_message('Comment cannot be blank.', 10);
			return;
		}
		postdata += '&comment=' + encodeURIComponent(elmt.value);
		
		
		var callback = function(json){
            if(json.status == 'OK'){
            	location.reload();
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'POST',
            'url' : jq_main_url + '/SubmitApp.do?dispatch=create_recom_comment',
            'postdata' : postdata
        };

        mapUI.jq_call(jq, callback);
	},
	
	create_recommendation : function(case_id) {
		var elmt, postdata;
		
		var data = {};
		
		postdata = 'case_id=' + encodeURIComponent(case_id);
		
		elmt = document.getElementById('case_outlet_opt');
		postdata += '&outlet_id=' + encodeURIComponent(elmt.value);
		
		
		
		var rate =  [];
		$('input[type=checkbox][name="rate"]').each(function(index, elmt){
            if($(this).is(':checked')){
            	rate.push($(this).val());
            }
        });
		data['rate'] = rate;
		
		elmt = document.getElementById('visa_rate');
		data['visa_rate'] = elmt.value;
		
		elmt = document.getElementById('jcb_rate');
		data['jcb_rate'] = elmt.value;
		
		elmt = document.getElementById('debit_rate');
		data['debit_rate'] = elmt.value;
		
		elmt = document.getElementById('amex_rate');
		data['amex_rate'] = elmt.value;
		
		elmt = document.getElementById('trf');
		data['trf'] = elmt.value;
		
		elmt = document.getElementById('curr_card_sales');
		data['curr_card_sales'] = elmt.value;
		
		elmt = document.getElementById('no_of_outlet');
		data['no_of_outlet'] = elmt.value;
		
		
		
		
		var terminal_edc = [];
		$('input[type=checkbox][name="terminal_edc"]').each(function(index, elmt){
            if($(this).is(':checked')){
            	terminal_edc.push($(this).val());
            }
        });
		data['terminal_edc'] = terminal_edc;
		
		elmt = document.getElementById('no_of_wired_terminal');
		data['no_of_wired_terminal'] = elmt.value;
		
		elmt = document.getElementById('no_of_wireless_terminal');
		data['no_of_wireless_terminal'] = elmt.value;
		
		elmt = document.getElementById('no_of_mpos_terminal');
		data['no_of_mpos_terminal'] = elmt.value;
		
		
		var terminal_rental = [];
		$('input[type=checkbox][name="terminal_rental"]').each(function(index, elmt){
            if($(this).is(':checked')){
            	terminal_rental.push($(this).val());
            }
        });
		data['terminal_rental'] = terminal_rental;
		
		
		var terminal_rental_amount = "";
		$('input[type=radio][name="terminal_rental_amount"]').each(function(index, elmt){
            if($(this).is(':checked')){
            	terminal_rental_amount = $(this).val();
            }
        });
		data['terminal_rental_amount'] = terminal_rental_amount;
		
		
		elmt = document.getElementById('terminal_rental_wired');
		data['terminal_rental_wired'] = elmt.value;
		
		elmt = document.getElementById('terminal_rental_wireless');
		data['terminal_rental_wireless'] = elmt.value;
		
		elmt = document.getElementById('terminal_rental_acc');
		data['terminal_rental_acc'] = elmt.value;
		
		elmt = document.getElementById('terminal_rental_bank');
		data['terminal_rental_bank'] = elmt.value;
		
		
		
		
		elmt = document.getElementById('min_card_sales');
		data['min_card_sales'] = elmt.value;
		
		
		
		
		elmt = document.getElementById('mbb_ca');
		data['mbb_ca'] = elmt.value;
		
		elmt = document.getElementById('avg_deposit');
		data['avg_deposit'] = elmt.value;
		
		elmt = document.getElementById('avg_credit_balance');
		data['avg_credit_balance'] = elmt.value;
		
		elmt = document.getElementById('referral');
		data['referral'] = elmt.value;
		
		elmt = document.getElementById('proven_card_sales');
		data['proven_card_sales'] = elmt.value;
		
		elmt = document.getElementById('projected_card_sales');
		data['projected_card_sales'] = elmt.value;
		
		postdata += "&recom=" + encodeURIComponent(JSON.stringify(data));
		
		
		data = {};
		
		$('table.mdr_rate').find('tbody').find('tr').find('td').each(function(index, elmt){
			var input = $(this).find('input[type="text"]:eq(0)');
			var select = $(this).find('select');
			
			if(input.length > 0) {
				//postdata += '&' + input.attr('name') + '=' + encodeURIComponent(input.val());
				data[input.attr('name')] = input.val();
			}
			else if(select.length > 0) {
				//console.log(select.attr('name'));
				//alert(select.attr('name'));
				data[select.attr('name')] = select.val();
			}
		});
		
		postdata += "&mdr=" + encodeURIComponent(JSON.stringify(data));
		
		var callback = function(json){
            if(json.status == 'OK'){
            	mapUI.jq_message("Recommendation has been saved successfully.", 10);
            	location.reload();
            } else {
            	mapUI.jq_message(json.message, 10);
            }
        };
		
        var jq = {
            'method' : 'POST',
            'url' : jq_main_url + '/SubmitApp.do?dispatch=create_recommendation',
            'postdata' : postdata
        };

        mapUI.jq_call(jq, callback);
	},
};